package controller;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import biz.MissionBiz;
import biz.ProjectManagerBiz;
import biz.UserBiz;
import pojo.MissionRelations;
import pojo.Missions;
import pojo.ProjectMissions;
import pojo.Users;
import pojo.view.ProjectDynmic;
import pojo.view.ProjectUser;
import pojo.view.ScoreProjectUser;
import util.DateTransfrom;
import util.PageUtil;
@Controller
public class ProjectManagerController {
	@Resource(name="projectManagerBiz")
	private ProjectManagerBiz projectManagerBiz;
	@Resource(name="userBiz")
	private UserBiz userBiz;
	@Resource(name="missionBiz")
	private MissionBiz missionBiz;
	
	@RequestMapping("projectUserFenYe.do")
	@ResponseBody
	public PageUtil<Users> projectUserFenYe(int projectid,int projectUserNowNo){
		System.out.println("projectUserFenYe.do000000000000000000000000");
		int projectUserTotalNo=projectManagerBiz.getProjectUserTotalNo(projectid);

		
		System.out.println("projectUserNowNo:"+projectUserNowNo);
		//团队成员模块
		List<Users> tempProjectUserList=projectManagerBiz.projectUserFenYe(projectUserNowNo, projectid);
		System.out.println("tempProjectUserList.size():"+tempProjectUserList);
		List<ProjectUser> projectUserListPreview=new ArrayList<>();
		if(tempProjectUserList!=null){
			 DecimalFormat df = new DecimalFormat("#.00"); 
			for (int i = 0; i < tempProjectUserList.size(); i++) {
				Users user=tempProjectUserList.get(i);
				ProjectUser tu=new ProjectUser();
				tu.setUserId(user.getUserId());
				tu.setUserName(user.getUserName());
			
				tu.setEmail(user.getEmail());
				tu.setTel(user.getTel());
				tu.setHeadimage(user.getHeadimage());
				tu.setSubjectScore(df.format(missionBiz.findRoleidByUrpid(String.valueOf(projectid), String.valueOf(user.getUserId())).getSubjectiveScore()));
				tu.setObjectScore(df.format(missionBiz.findRoleidByUrpid(String.valueOf(projectid), String.valueOf(user.getUserId())).getObjectiveScore()));
				tu.setProjectScore(df.format(missionBiz.findRoleidByUrpid(String.valueOf(projectid), String.valueOf(user.getUserId())).getSubjectiveScore()+missionBiz.findRoleidByUrpid(String.valueOf(projectid), String.valueOf(user.getUserId())).getObjectiveScore()));
				//
				tu.setTeamUserNowNo(projectUserNowNo);
				tu.setTeamUserTotalNo(projectUserTotalNo);
				projectUserListPreview.add(tu);
			}
		}
		
		//测试用的
		PageUtil<Users> page=new PageUtil<Users>();
		 page.setAt(projectUserNowNo);//当前页
		 page.setList(projectUserListPreview);
		 page.setPages(projectUserTotalNo);//总页数
		return page;
	}
	//项目阶段任务模块
	@RequestMapping("projectMission.do")
	@ResponseBody
	public List<ProjectMissions> getAllProjectMissionByProjectId(int projectid){
		System.out.println("projectMissiondo000000000000000000000000"+projectid);
		List<ProjectMissions> tempProjectMissionList=projectManagerBiz.getAllProjectMissionByProjectId(projectid);
		return tempProjectMissionList;
	}

	
	@RequestMapping("projectMissionDetail.do")
	@ResponseBody
	public ProjectMissions projectMission(int projectmissiontid){
		System.out.println("projectMissionDetail:"+projectmissiontid);
		ProjectMissions projectMission=projectManagerBiz.getProjectMissionByProMisId(projectmissiontid);
		
		 SimpleDateFormat dateformat1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 String date;
		 if (projectMission.getStartDate()!=null) {
			 date=dateformat1.format(projectMission.getStartDate());
		}else date="";
		 projectMission.setStrDate(date);
		 
		 if (projectMission.getCompleteDate()!=null) {
			 date=dateformat1.format(projectMission.getCompleteDate());
		}else date="";
		 projectMission.setStrCDate(date);
		return projectMission;
	}
	
	@RequestMapping("addProjectMission.do")
	@ResponseBody
	public List<ProjectMissions> addProjectMission(String missionName, String description, String arr, String projectId,String roleId,HttpSession session) {
		Users currentUser = (Users) session.getAttribute("currentUser");
		MissionRelations mr = new MissionRelations();
		//System.out.println(arr);
		int urpid= userBiz.findUrpidByURP(String.valueOf(currentUser.getUserId()), roleId, projectId).getUrpid();
		ProjectMissions projectmission = new ProjectMissions();
		projectmission.setMissionName(missionName);
		projectmission.setDescription(description);
		projectmission.setUrpid(urpid);

		System.out.println("addProjectMissiondo000000000000000000000000");
		projectManagerBiz.addProjectMissionAndGetId(projectmission);

		// 获取到的即为新插入记录的ID
		int aftermissionid = projectmission.getProjectMissionId();
		

		// 插入关联表
		if (arr != "") {
			System.out.println("arr的内容是"+arr);
			String[] arrArray = arr.split(",");
			for (int i = 0; i < arrArray.length; i++) {
				System.out.println("arr的内容遍历"+arrArray[i]);
				mr.setFrontMissionName(arrArray[i]);
				mr.setAfterMissionName(missionName);
				mr.setUrpid(urpid);
				projectManagerBiz.addRelationalTable(mr);
				//frontmissionid = Integer.parseInt(arrArray[i]);
				//projectManagerBiz.addRelationalTable(frontmissionid, aftermissionid);
				
			}
		}else{
			System.out.println("arr有错误");
		}
		
		//通过projectmissiontid获得projectid
		System.out.println("urpid:"+projectManagerBiz.getUrpIdByProMisId(aftermissionid));
		int projectid = projectManagerBiz.getProjectIdByUrpId(urpid);
		System.out.println("projectid:"+projectid);
		//通过projectid获得ProjectMssion列表
		List<ProjectMissions> tempProjectMissionList=projectManagerBiz.getAllProjectMissionByProjectId(projectid);
		return tempProjectMissionList;
	}
	
	@RequestMapping("addMission.do")
	@ResponseBody
	public List<Missions> addMission(String missionName, String description, String ETC, int urpid,int projectmissiontid) throws ParseException {
		System.out.println(DateTransfrom.spliteStrForDate(ETC));
		String datestr=DateTransfrom.spliteStrForDate(ETC);
		Date date = DateTransfrom.transStrToDate(datestr);
		/*SimpleDateFormat sdf = new SipleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = sdf.parse(ETC);*/
		
		
//		Timestamp timestamp= new Timestamp(date.getTime());

		
		projectManagerBiz.addMissionAndGetId(missionName, description, date, urpid, projectmissiontid);
		
		List<Missions> tempMissionList=projectManagerBiz.getAllMissionByProMisId(projectmissiontid);
		return tempMissionList;
	}
	
	@RequestMapping("getMissionAllocate.do")
	@ResponseBody
	public List<Missions> getAllMissionProMisId(int projectmissionid) {
		
		System.out.println(projectmissionid);
		
		List<Missions> tempMissionList=projectManagerBiz.getAllMissionByProMisId(projectmissionid);
		return tempMissionList;
	}
	
	
	//通过projectmissiontid获得usersDetail
	@RequestMapping("projectUserList.do")
	@ResponseBody
	public List<ProjectUser> projectUserList(int projectmissionid) {
		System.out.println("getUserNameByProMisId:" + projectmissionid);
		int urpid=projectManagerBiz.getUrpIdByProMisId(projectmissionid);
		int projectid = projectManagerBiz.getProjectIdByUrpId(urpid);
		List<ProjectUser> tempUserList = projectManagerBiz.getAllUsersByProjectId(projectid);
		
		return tempUserList;
	}
	
	@RequestMapping("scoreProjectMember.do")
	@ResponseBody
	public ScoreProjectUser scoreProjectMember(int userid,int projectid) {
		System.out.println("scoreProjectMember.do:"+userid);
		int urpid=projectManagerBiz.getUrpIdByUidAndPid(userid,projectid);
		System.out.println("urpid:" + urpid);
		ScoreProjectUser scoreProjectUser = projectManagerBiz.getScoreUserByUrpId(urpid);
		
		return scoreProjectUser;
	}
	
	@RequestMapping("scoreProMember.do")
	@ResponseBody
	public void scoreProMember(int urpid,int subjectivescore,int objectivescore) {
		System.out.println("scoreProjectMember.do:"+"score"+subjectivescore/2+objectivescore/2);
		
		System.out.println("urpid:" + urpid);
		projectManagerBiz.scoreProMember(urpid,subjectivescore);
	}
	
	//项目成员模块
		@RequestMapping("projectUser.do")
		public String queryProjectUserByTeamId(String projectId,HttpSession session,HttpServletRequest request){
			//前台传来的应该是urpid 可以知道项目id 根据项目id查询项目成员
			//团队成员模块
					List<Users> tempprojectUserList=projectManagerBiz.projectUserFenYe(1,Integer.parseInt(projectId));
					List<ProjectUser> projectUserListPreview=new ArrayList<>();
					int projectUserNowNo=1;
					int projectUserTotalNo=projectManagerBiz.getProjectUserTotalNo(Integer.parseInt(projectId));
					if(tempprojectUserList!=null){
						 DecimalFormat df = new DecimalFormat("#.00"); 
						for (int i = 0; i < tempprojectUserList.size(); i++) {
							Users user=tempprojectUserList.get(i);
							ProjectUser tu=new ProjectUser();
							tu.setUserId(user.getUserId());
							tu.setUserName(user.getUserName());
						
							tu.setEmail(user.getEmail());
							tu.setTel(user.getTel());
							tu.setHeadimage(user.getHeadimage());
							tu.setSubjectScore(df.format(missionBiz.findRoleidByUrpid(String.valueOf(projectId), String.valueOf(user.getUserId())).getSubjectiveScore()));
							tu.setObjectScore(df.format(missionBiz.findRoleidByUrpid(String.valueOf(projectId), String.valueOf(user.getUserId())).getObjectiveScore()));
							tu.setProjectScore(df.format(missionBiz.findRoleidByUrpid(String.valueOf(projectId), String.valueOf(user.getUserId())).getSubjectiveScore()+missionBiz.findRoleidByUrpid(String.valueOf(projectId), String.valueOf(user.getUserId())).getObjectiveScore()));
							//
							tu.setTeamUserNowNo(projectUserNowNo);
							tu.setTeamUserTotalNo(projectUserTotalNo);
							projectUserListPreview.add(tu);
						}
					}
					//测试用的
					request.setAttribute("nowprojectid", projectId);
					
					//测试结束
					request.setAttribute("projectUserList", projectUserListPreview);
					request.setAttribute("projectUserNowNo", projectUserNowNo);
					request.setAttribute("projectUserTotalNo", projectUserTotalNo);
			
			
			return "xmcy";
		}
		
		@RequestMapping("gotoxmjl.do")
		public String gotoxmjl(String projectId,String roleId,HttpSession session,HttpServletRequest request) {
			request.setAttribute("xmprojectId", projectId);
			request.setAttribute("xmroleId", roleId);
			return "xmjl1";
		}
		
		@ResponseBody
		@RequestMapping("getProgressbarInfo.do")
		public String findPMinfoByUserid(String projectId,HttpServletResponse response){
//			response.setCharacterEncoding("utf-8");
//			PrintWriter out=null;
//			ObjectMapper mapper=new ObjectMapper();
//			List<Projects> projectsList = missionBiz.findProjectsByUrpid(teamId, userId);
//			System.out.println(projectsList.get(0).getProjectName());
/*			try {
				out=response.getWriter();
				out.println(projectManagerBiz.getPrograsePersent(projectid));
			} catch (JsonProcessingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}*/
			System.out.println("进读条controller获取的projectid是"+projectId);
			System.out.println(projectManagerBiz.getPrograsePersent(projectId));
			return projectManagerBiz.getPrograsePersent(projectId);
		}
		
		//项目动态模块
		@RequestMapping("projectDynamics.do")
		@ResponseBody
		public List<ProjectDynmic> projectDynamicList(int projectid) {
			System.out.println("projectDynamics.do:" +"projectid:"+ projectid);
			
			List<ProjectDynmic> tempDynamicList = projectManagerBiz.getAllProjectDynamic(projectid);
			if(tempDynamicList.isEmpty()){
				System.out.println("返回的tempDynamicList是空的");
			}else{
				for (int i = 0; i < tempDynamicList.size(); i++) {
					
					SimpleDateFormat dateformat1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				 	String date;
				 	if (tempDynamicList.get(i).getAft()!=null) {
						 date=dateformat1.format(tempDynamicList.get(i).getAft());
					}else date="";
				 		tempDynamicList.get(i).setStrAft(date);
				}
			}
			return tempDynamicList;
		}
}
	

